﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

using System.ComponentModel;

namespace HA.Data
{

    /// <summary>
    /// 用于表示二维离散点集合
    /// </summary>
    [Serializable]
   public class Scatter2DCollection
    {
        public Scatter2DCollection()
        {
            _datas = new List<Scatter2D>();
        }

        #region 对外事件
        [field: NonSerialized]
        public event EventHandler<CollecionChangedEventArgs> CollectionChanged;


        [field: NonSerialized]
        public event EventHandler<Scatter2DPointsChangedEventArgs> Scatter2DChanged; 

        #endregion


        List<Scatter2D> _datas;

        /// <summary>
        /// 只允许读数据，不允许在这里添加
        /// </summary>
        public List<Scatter2D> Datas
        {
            get { return _datas; }
          
        }

        int _indexBase = 0;

        protected string GetNextId()
        {
            this._indexBase++;

            return "数据" + this._indexBase.ToString();
        }



        #region 公共函数

        public Scatter2D CreateNew()
        {


            Scatter2D scatter = new Scatter2D(this.GetNextId());


            return scatter;
        }


        public void Add(Scatter2D data)
        {
            this._datas.Add(data);
            data.ParentCollection = this;

            if (this.CollectionChanged!=null)
            {
                this.CollectionChanged(this,new CollecionChangedEventArgs(data,CollectionChangedType.ADD));
                
            }

        }

        public void Remove(Scatter2D data)
        {
            if (this._datas.Contains(data))
            {
                this._datas.Remove(data);
                data.ParentCollection = null;

                if (this.CollectionChanged != null)
                {
                    this.CollectionChanged(this, new CollecionChangedEventArgs(data, CollectionChangedType.REMOVE));

                }
                
            }
        }

        public void Replace(string id,Scatter2D data)
        {
            foreach (Scatter2D item in _datas)
            {
                if (item.Key.ToString() == id)
                {
                    item.ReSetMembers(data);

                    if (this.CollectionChanged != null)
                    {
                        this.CollectionChanged(this, new CollecionChangedEventArgs(data, CollectionChangedType.REPLACE));
                    }
                    return;
                }
            }
        }

        public void AskScatter2DChanged(Scatter2DPointsChangedEventArgs e)
        {
            if (this.Scatter2DChanged != null)
                this.Scatter2DChanged(this, e);
        }

        public Scatter2D GetDataById(string id)
        {
            foreach (Scatter2D item in this.Datas)
            {
                if (item.Key.ToString() == id)
                    return item;
            }

            return null;
        }

        #endregion




    }
}
